package com.acwing.partition21;

import java.io.*;
import java.util.HashSet;
import java.util.Set;

/**
 * @author `RKC`
 * @date 2022/1/3 20:03
 */
public class AC2058笨拙的手指 {

    private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    private static final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        String two = reader.readLine(), three = reader.readLine();
        writer.write(resolve(two, three) + "\n");
        writer.flush();
    }

    private static int resolve(String two, String three) {
        Set<Integer> set = new HashSet<>();
        //枚举二进制的每一位，每次只写错一位
        for (int i = 0; i < two.length(); i++) {
            for (char j = '0'; j <= '1'; j++) {
                if (j != two.charAt(i)) {
                    char[] arr = two.toCharArray();
                    arr[i] = j;
                    //转成十进制后放入到set中
                    set.add(kthToTen(new String(arr), 2));
                }
            }
        }
        //枚举三进制的每一位，每次只写错一位
        for (int i = 0; i < three.length(); i++) {
            for (char j = '0'; j <= '2'; j++) {
                if (j != three.charAt(i)) {
                    char[] arr = three.toCharArray();
                    arr[i] = j;
                    int num = kthToTen(new String(arr), 3);
                    if (set.contains(num)) return num;
                }
            }
        }
        return -1;
    }

    private static int kthToTen(String s, int k) {
        int res = 0;
        for (int i = s.length() - 1, j = 0; i >= 0; i--, j++) res += (s.charAt(i) - '0') * Math.pow(k, j);
        return res;
    }
}
